package com.finance.framework.lang.valid;

import com.alibaba.druid.util.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.text.ParseException;
import java.text.SimpleDateFormat;

/**
 * Created by ljg on 2017/4/7.
 */
public class DateValidImpl implements ConstraintValidator<DateValid, String> {
    private String regexp;
    @Override
    public void initialize(DateValid dateValid) {
        this.regexp=dateValid.regexp();
    }

    @Override
    public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
        if(StringUtils.isEmpty(s)){
            return true;
        }
        SimpleDateFormat sdf=new SimpleDateFormat(regexp);
        try {
            sdf.setLenient(false);
            sdf.parse(s);
            return true;
        } catch (ParseException e) {
            return false;
        }
    }
}
